﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RVEAd.Helpers;
using System.Configuration;
using RVEAd.Models;

namespace RVEAd.Forms
{
    public partial class FormAlegeSpot : Form
    {
        #region Properties

        public List<SpotFile> SpotFiles { get; set; }

        public Models.SpotFile SelectedSpotFile { get; set; }
        public bool UserSelectedFolder { get; set; }

        #endregion

        #region Ctor

        public FormAlegeSpot()
        {
            InitializeComponent();
            this.Load += new EventHandler(FormAlegeSpot_Load);
        }

        #endregion

        #region Event Handlers

        void FormAlegeSpot_Load(object sender, EventArgs e)
        {
            InitialLoad();
        }

        void btnLoadFolder_Click(object sender, EventArgs e)
        {
            LoadFromUserSpecifiedFolder();
        }

        void tbSearchSpot_TextChanged(object sender, EventArgs e)
        {
            if (UserSelectedFolder)
            {
                dataGridSpot.DataSource = SpotFiles
                    .Where(f => f.Description.Contains(tbSearchSpot.Text))
                    .ToList();
            }
            else
            {
                dataGridSpot.DataSource = Helpers.FileSystem.GetFolderFiles(Config.SpotsFolder, tbSearchSpot.Text);
            }
        }

        void btnSelecteaza_Click(object sender, EventArgs e)
        {
            SelecteazaSpot();
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            SelecteazaSpot();
        }

        void dataGridSpot_DoubleClick(object sender, EventArgs e)
        {
            SelecteazaSpot();
        }

        #endregion

        #region Private methods

        private void InitialLoad()
        {
            UserSelectedFolder = false;
            SpotFiles = Helpers.FileSystem.GetFolderFiles(Config.SpotsFolder, string.Empty);
            dataGridSpot.DataSource = SpotFiles;
        }

        private void SelecteazaSpot()
        {
            this.SelectedSpotFile = (Models.SpotFile)dataGridSpot.CurrentRow.DataBoundItem;
            this.Close();
        }

        private void LoadFromUserSpecifiedFolder()
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            DialogResult result = folderDialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                UserSelectedFolder = true;
                SpotFiles = Helpers.FileSystem.GetFolderFiles(folderDialog.SelectedPath, string.Empty);
                dataGridSpot.DataSource = SpotFiles;
            }
        }

        #endregion

        private void btnMarkDeCitit_Click(object sender, EventArgs e)
        {
            this.SelectedSpotFile = new Models.SpotFile { Description = ConfigurationManager.AppSettings["DeCititToken"] ?? "De citit" };
            this.Close();
        }
    }
}
